<?php

namespace app\admin\controller;

use think\Request;
use think\Db;
use think\Controller;
use think\Log;
class Product extends Controller
{
	public function products_list()
	{	$title='商品列表';
        $goods=Db::name('goods')->where('isfall',1)->select();
        $goodscount=Db::name('goods')->where('isfall',1)->count();
        //dump($goods);
        $this->assign('title',$title);
        $this->assign('goods',$goods);
        $this->assign('goodscount',$goodscount);
		return $this->fetch();
	}	
	public function down($gid)
	{   
		$isfall=Db::name('goods')->where('gid',$gid)->value('isfall');
		//echo $isfall;
		//exit;
		if($isfall){
			$updown=Db::name('goods')->where('gid',$gid)->setField('isfall',0);
		}else{
			$updown=Db::name('goods')->where('gid',$gid)->setField('isfall',1);
		}
		$this->success('下架成功！','products_list');
	}
	public function up($gid)
	{   
		$isfall=Db::name('goods')->where('gid',$gid)->value('isfall');
		//echo $isfall;
		//exit;
		if($isfall){
			$updown=Db::name('goods')->where('gid',$gid)->setField('isfall',0);
		}else{
			$updown=Db::name('goods')->where('gid',$gid)->setField('isfall',1);
		}
		$this->success('上架成功！','brand_manage');
	}	
	public function category_manage()
	{
		return $this->fetch();
	}
	public function brand_manage()
	{   
        $title="商品回收";
		$data=Db::name('goods')->where('isfall',0)->select();
		$count=Db::name('goods')->where('isfall',0)->count();
		$this->assign('data',$data);
		$this->assign('count',$count);
		return $this->fetch();
	}
	public function product_add()
	{

	// 获取表单上传文件?例如上传了001.jpg
		$file = request()->file('public');
		if($file){
	// 移动到框架应用根目录/public/uploads/?目录下
		$info = $file->move(ROOT_PATH . 'public' . DS . 'image');

		if($info){
	// 成功上传后?获取上传信息
	// 输出 jpg
		//echo $info->getExtension();
	//?输出?20160820/42a79759f284b767dfcb2a0197904287.jpg
		//echo $info->getSaveName();
	//?输出?42a79759f284b767dfcb2a0197904287.jpg
		//echo $info->getFilename();
		$public=$info->getSaveName();
		//dump($_POST);
	    $goodsname=$_POST['goodsname'];
		$price=$_POST['price'];
		$summary=$_POST['summary'];
		$nowprice=$_POST['nowprice'];
		$classify=$_POST['class'];
		$describe=$_POST['describe'];
		$update_time=time();
		$create_time=time();
		$data=[
			'goodsname'=>$goodsname,
			'summary'=>$summary,
			'price'=>$price,
			'update_time'=>$update_time,
			'create_time'=>$create_time,
			'public'=>$public,
			'nowprice'=>$nowprice,
			'classify'=>$classify,
			'describe'=>$describe,
		];
		$insert=Db::name('goods')->insert($data);
		if($insert){
			$this->success('添加成功！','products_list');
		}else{
			$this->success('添加失败！','products_list');
			return $this->fetch();
		}
	}else{
	//?上传失败获取错误信息
		echo $file->getError();
						}
			}
		return $this->fetch();
		
	}
	public function product_category_add()
	{
		return $this->fetch();
	}
	public function product_edit($gid)
	{   
		
		$this->assign('gid',$gid);
		return $this->fetch();
	}
	public function product_editt()
	{   
		return $this->fetch();
	}
	public function product_edit_submit()
	{
	    //dump($_POST);
	    //exit;
		// 获取表单上传文件?例如上传了001.jpg
		$file = request()->file('public');
		if($file){
	// 移动到框架应用根目录/public/uploads/?目录下
		$info = $file->move(ROOT_PATH . 'public' . DS . 'image');

		if($info){
	// 成功上传后?获取上传信息
	// 输出 jpg
		//echo $info->getExtension();
	//?输出?20160820/42a79759f284b767dfcb2a0197904287.jpg
		//echo $info->getSaveName();
	//?输出?42a79759f284b767dfcb2a0197904287.jpg
		//echo $info->getFilename();
		$public=$info->getSaveName();
	    $goodsname=$_POST['goodsname'];
		$price=$_POST['price'];
		$gid=$_POST['gid'];
		$summary=$_POST['summary'];
		$nowprice=$_POST['nowprice'];
		$classify=$_POST['class'];
		$describe=$_POST['describe'];
		$update_time=time();
		$create_time=time();
		$data=[
			'goodsname'=>$goodsname,
			'summary'=>$summary,
			'price'=>$price,
			'update_time'=>$update_time,
			'create_time'=>$create_time,
			'public'=>$public,
			'nowprice'=>$nowprice,
			'classify'=>$classify,
			'describe'=>$describe,
		];
		$update=Db::name('goods')->where('gid',$gid)->update([
			'goodsname'=>$goodsname,
			'summary'=>$summary,
			'price'=>$price,
			'update_time'=>$update_time,
			'create_time'=>$create_time,
			'public'=>$public,
			'nowprice'=>$nowprice,
			'classify'=>$classify,
			'describe'=>$describe,
		]);
		if($update){
			$this->success('修改成功！','THINK_URL/admin/product/product_editt');
		}else{
			$this->success('修改失败！','THINK_URL/admin/product/product_editt');
			return $this->fetch();
		}
	}else{
	//?上传失败获取错误信息
		echo $file->getError();
						}
			}
	}
	public function product_delete($gid)
	{   
		$delete=Db::name('goods')->where('gid',$gid)->delete();
		if($delete){
			$this->success('删除成功！','products_list');
		}
	}
}